﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using HKB.Common;
using HKB.Common.HKB.Common;
using HKB.Data;
using HKB.Data.Bll;
using HKB.Web;
using HKB.Web.Shop.Member;

public partial class Member_Shop_ReceiveAddress : Member
{
    private Shop_Address _addr;
    protected Shop_Address m_addr
    {
        get
        {
            Shop_Address addr;
            if (this._addr == null)
            {
                this._addr = new Shop_Address(this.conn);
                addr = this._addr;
            }
            else
            {
                addr = this._addr;
            }
            return addr;
        }
    }

    private Shop_City _mycity = null;
    protected Shop_City mycity
    {
        get
        {
            Shop_City mycity;
            if (this._mycity == null)
            {
                this._mycity = new Shop_City(this.conn);
                mycity = this._mycity;
            }
            else
            {
                mycity = this._mycity;
            }
            return mycity;
        }
    }
    public int Num = 10;

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!base.IsPostBack)
        {
            this.lblUserName.Text = base.user.UserName;
            DataTable dataTable = BLLInstance.BllShoppingCarts.GetList(" userID=" + base.user.UserID).Tables[0];
            this.lblShopCarNum.Text = dataTable.Rows.Count.ToString();
            this.Buyer_Order.Text = GetRemindMe();
            this.BindPage();

            this.BindProList();
        }
    }

    protected void BindPage()
    {
        DataTable dt = this.m_addr.GetAddress(base.user.UserID);
        this.Repeater1.DataSource = dt;
        this.Repeater1.DataBind();
    }

    #region 省市县联动
    protected void BindProList()
    {
        this.Province.DataSource = this.mycity.GetAllProvince();
        this.Province.DataBind();
    }
    protected void BindCityList()
    {
        this.City.DataSource = this.mycity.GetCity(this.Province.SelectedValue);
        this.City.DataBind();
    }
    protected void SelectCity(object sender, EventArgs e)
    {
        string selectedValue = this.Province.SelectedValue;
        this.BindCityList();
        this.BindAreaList();
    }
    protected void BindAreaList()
    {
        this.ddlArea.DataSource = this.mycity.GetAllAreaByCity(this.City.SelectedValue);
        this.ddlArea.DataBind();
    }
    protected void SelectArea(object sender, EventArgs e)
    {
        this.BindAreaList();
    }
    #endregion

    protected void Repeater1_ItemCommand(object source, RepeaterCommandEventArgs e)
    {
        if (e.CommandName == "Edit")
        {
            HiddenField hfldId = e.Item.FindControl("hfldAddressId") as HiddenField;
            if (hfldId != null)
            {
                DataTable dt = this.m_addr.GetAddressByID(int.Parse(hfldId.Value));
                if (dt.Rows.Count == 1)
                {
                    this.hfldId.Value = dt.Rows[0]["AddressID"].ToString();
                    this.UserName.Value = dt.Rows[0]["UserName"].ToString();
                    this.Province.SelectedValue = dt.Rows[0]["Province"].ToString();
                    this.BindCityList();
                    this.City.SelectedValue = dt.Rows[0]["City"].ToString();
                    this.BindAreaList();
                    this.ddlArea.SelectedValue = dt.Rows[0]["CMArea"].ToString();
                    this.Address.Value = dt.Rows[0]["Address"].ToString();
                    this.ZipCode.Value = dt.Rows[0]["ZipCode"].ToString();
                    this.ConectPhone.Value = dt.Rows[0]["ConectPhone"].ToString();
                    this.MobilePhone.Value = dt.Rows[0]["MobilePhone"].ToString();
                    this.ibtn.ImageUrl = "/images/tijiao3.jpg";
                }
            }
        }
        else if (e.CommandName == "Delete")
        {
            HiddenField hfldId = (HiddenField)e.Item.FindControl("hfldAddressId");
            if (hfldId != null)
            {
                this.m_addr.DeleteAddress(int.Parse(hfldId.Value));
                this.WriteSuccessMsg("", Language.GetMemberMsg("Common/DeleteSuccess", new string[0]), "Shop_ReceiveAddress.aspx");
            }
        }
        //this.BindPage();
    }
    protected void Repeater1_ItemDataBound(object sender, RepeaterItemEventArgs e)
    {
        DataRowView drv = (DataRowView)e.Item.DataItem;
        if (drv["IsDefault"].ToString().Equals("1"))
        {
            ((RadioButton) e.Item.FindControl("IsDefault")).Checked = true;
        }
    }
    protected void SetDefaultAddress_CheckedChanged(object sender, EventArgs e)
    {
        RadioButton rdb = sender as RadioButton;
        RepeaterItem item = rdb.Parent as RepeaterItem;

        HiddenField hfldId = Repeater1.Items[item.ItemIndex].FindControl("hfldAddressId") as HiddenField;

        if (hfldId != null)
        {
            this.m_addr.UpdateDefaultAddress(int.Parse(hfldId.Value));
            this.BindPage();
        }
    }

    protected void ibtn_Click(object sender, ImageClickEventArgs e)
    {
        int UserID = base.user.UserID;
        string UserName = this.UserName.Value;
        string Address = this.Address.Value;
        string ZipCode = this.ZipCode.Value;
        string strConectPhone = this.ConectPhone.Value;
        string MobilePhone = this.MobilePhone.Value;
        string Email = string.Empty;
        string Province = this.Province.SelectedValue;
        string strCity=string.Empty;
        string strCMArea = string.Empty;
        if (Province != "")
        {
            strCity = this.City.SelectedValue;
            strCMArea = this.ddlArea.SelectedValue;
        }
        Int16 IsDefault = 0;

        if (this.hfldId.Value.Length == 0) //新增
        {
            try
            {
                Num = int.Parse(this.Addrnum.Text);
            }
            catch { }

            if (!this.m_addr.IsMaxCount(base.user.UserID, Num))
            {
                this.m_addr.InsertAddress(UserID, UserName, Address, ZipCode, strConectPhone, MobilePhone, Email, Province,
                                          strCity, strCMArea, IsDefault);
                this.WriteSuccessMsg("", Language.GetMemberMsg("Common/AddSuccess", new string[0]), "Shop_ReceiveAddress.aspx");
            }
            else
            {
                this.WriteErrMsg("", Language.GetMemberMsg("Address/AddNoMore", new string[0]), "");
            }
        }
        else
        {
            this.m_addr.UpdateAddressByAddressID(int.Parse(this.hfldId.Value), UserName, Address, ZipCode, strConectPhone, MobilePhone, Email, Province,
                                      strCity, strCMArea, IsDefault);
            this.WriteSuccessMsg("", Language.GetMemberMsg("Common/EditSuccess", new string[0]), "Shop_ReceiveAddress.aspx");
        }

        
    }

}